﻿using System;
using System.IO;
using System.Linq;

namespace Homework17
{
    internal class Program
    {
        const string FILENAME = "D:\\CSharpHomework\\csharp2024\\Homework16\\Data.txt"; // 文件读取路径

        /// <summary>
        /// 主程序入口，读取文件并计算平均年龄和平均身高。
        /// </summary>
        static void Main(string[] args)
        {
            var dataEntries = ReadDataFromFile(); // 从文件读取数据条目
            var maleStats = CalculateStatistics(dataEntries.Where(entry => entry.Gender == "Male")); // 计算男性统计数据
            var femaleStats = CalculateStatistics(dataEntries.Where(entry => entry.Gender == "Female")); // 计算女性统计数据

            // 输出统计结果
            Console.WriteLine("Male Average Age: " + maleStats.AverageAge);
            Console.WriteLine("Male Average Height: " + maleStats.AverageHeight);
            Console.WriteLine("Female Average Age: " + femaleStats.AverageAge);
            Console.WriteLine("Female Average Height: " + femaleStats.AverageHeight);
        }

        // 从文件读取数据条目
        static List<DataEntry> ReadDataFromFile()
        {
            var dataEntries = new List<DataEntry>();
            using (StreamReader reader = new StreamReader(FILENAME))
            {
                string? line;
                reader.ReadLine(); // 跳过文件头部
                while ((line = reader.ReadLine()) != null)
                {
                    var values = line.Split(',');
                    dataEntries.Add(new DataEntry
                    {
                        Id = int.Parse(values[0]),
                        Name = values[1],
                        Age = int.Parse(values[2]),
                        Birthday = DateTime.Parse(values[3]),
                        Height = int.Parse(values[4]), // 身高作为整数解析
                        Gender = values[5]
                    });
                }
            }
            return dataEntries;
        }

        // 计算平均年龄和平均身高
        static Statistics CalculateStatistics(IEnumerable<DataEntry> entries)
        {
            double sumAge = 0;
            double sumHeight = 0;
            int count = 0;
            foreach (var entry in entries)
            {
                sumAge += entry.Age;
                sumHeight += entry.Height;
                count++;
            }
            return new Statistics
            {
                AverageAge = count > 0 ? sumAge / count : 0,
                AverageHeight = count > 0 ? sumHeight / count : 0
            };
        }

        class DataEntry
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime Birthday { get; set; }
            public int Height { get; set; } // 身高作为整数
            public string Gender { get; set; }
        }

        class Statistics
        {
            public double AverageAge { get; set; }
            public double AverageHeight { get; set; }
        }
    }
}